#include "../mars/thread_pool.h"
#include "../mars/logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <boost/bind.hpp>

void task(const char* task_name) {
    int count = 5;
    for (int i = 0; i < count; ++i) {
        INFO_PRINT("task_id:%s tid:%d, name:%s, main_thread:%d, count:%d\n", 
                task_name,
                mars::CurrentThread::tid(),
                mars::CurrentThread::name(),
                mars::CurrentThread::is_main_thread(),
                i);
        sleep(1);
    }
}


int main() {
    {
        mars::ThreadPool pool;
        pool.start(20);
        pool.run(boost::bind(task, "allen"));
        pool.run(boost::bind(task, "luffy"));
        sleep(2);
        pool.run(boost::bind(task, "zoro"));
        pool.stop();
        pool.run(boost::bind(task, "sanji"));
        pool.run(boost::bind(task, "nami"));
        pool.run(boost::bind(task, "robin"));

        // pool 在被销毁时会阻塞，它会等待所有执行中的任务结束，再回收资源
    }

    WARN_PRINT("pool destroyed\n");

    task("main");

    return 0;
}
